import { useEffect, useMemo } from "react";
import { useLocation, useMatches } from "react-router-dom";

let history = null;

/**
 * 获取历史+当前路由信息
 *
 * @returns [history, current]
 */
const useHistoryLocation = () => {
  const location = useLocation();
  const match = useMatches();

  const current = useMemo(() => {
    let data = location;
    if (location.pathname) {
      data.meta = match.find((item) => item.pathname === location.pathname)?.handle;
    }
    return data;
  }, [location, match]);

  useEffect(() => {
    history = location;
  }, [location]);

  return [history, current];
};

export default useHistoryLocation;
